﻿<?php 
$v_url = "/record/handle/index.php?modal_dialog_mode=1" . "&hdn_record_work_id=" . $v_record_work_id . "&fuseaction=DISPLAY_SINGLE_RECORD_WORK" ;
$v_recordtype_filter =$_SESSION['onegate_current_recordtype'];
if(isset($_REQUEST['hdn_recordtype_filter'])){
	$v_recordtype_filter = $_REQUEST['hdn_recordtype_filter'];
}
$v_record_work_id = intval($_REQUEST['hdn_item_id']);
$v_record_id = 0;
if(isset($_REQUEST['hdn_record_id'])){
	$v_record_id = intval($_REQUEST['hdn_record_id']);
}
//ID nguoi thuc hien cong viec
$v_worker_id = _get_item_attr_by_id($_SESSION['arr_all_staff'],$_SESSION['staff_id'],'id');
$v_worktype=$_REQUEST['hdn_work'];
$v_date= $_REQUEST['txt_date'];
$v_result =  _replace_bad_char($_REQUEST['txt_result']);
$v_file_id = "";	
//CSDL SQL
if(_is_sqlserver()){
	$v_form_field = 'file_attach';
	if (isset($_FILES[$v_form_field]['tmp_name'])){
		$v_record_work_ = $v_record_work_id;
		if($v_record_work_id==0||$v_record_work_id==''||empty($v_record_work_id)){
			$sql = "SELECT MAX(PK_RECORD_WORK) AS NEW_RECORD_WORK FROM dbo.T_ONEGATE_RECORD_WORK";
			$result = mssql_query($sql,$conn);
			$rs = mssql_fetch_array($result);
			$v_record_work_ = $rs['NEW_RECORD_WORK']+1;
		}
		$v_filename = $v_record_id.'_'.$v_record_work_.'_work_'._replace_bad_char(trim($_FILES[$v_form_field]['name']));
		$v_tmp_filename = trim($_FILES[$v_form_field]['tmp_name']);
		$v_file_id = 0;
		if ($v_tmp_filename!="" and !is_null($v_tmp_filename)){
			_copy_file($v_tmp_filename, _CONST_LEVEL2_ATTACH_FILE_PATH_FROM_CURRENT.$v_filename);
			if ($v_file_id ==""){
				$v_file_id = _save_file_to_database('T_TEMP_FILE', 'C_FILE_NAME', 'C_FILE_CONTENT', _CONST_LEVEL2_ATTACH_FILE_PATH_FROM_CURRENT.$v_filename);
			}else{			
				$v_file_id = $v_file_id . ",". _save_file_to_database('T_TEMP_FILE', 'C_FILE_NAME', 'C_FILE_CONTENT', _CONST_LEVEL2_ATTACH_FILE_PATH_FROM_CURRENT.$v_filename);
			}	
		}	
	}
	//nieuvv--111108--update tien do cua cong viec dac biet	
	$arr_rad_work = explode(",",$v_worktype);	
	//the-end-nieuvv--111108--
	if($arr_rad_work[2] == "SPEC")		// neu la cong viec dac biet
	{
		$arr_record_id = explode(",",$v_record_id);		
		for($j = 0 ; $j < sizeof($arr_record_id); $j++)
		{
			$sql = create_sql_string_for_update_record_work($arr_rad_work[0], $arr_record_id[$j], $v_worker_id, $v_date);			
			$ado_conn->SetFetchMode(ADODB_FETCH_ASSOC);
			$rs = $ado_conn->GetRow($sql); 
			$v_error = _replace_bad_char(trim($rs['RET_ERROR']));
		}
	}
	else
	{
		$v_result_id = _save_text_to_database('T_TEMP_FILE', 'C_TEXT', $v_result);
		$sql = "Exec Onegate_RecordWorkUpdate " ;
		$sql = $sql . $v_record_work_id;
		$sql = $sql . "," . $v_record_id ;
		$sql = $sql . "," . $v_worker_id;//ID nguoi thuc hien cong viec
		$sql = $sql . ",'" . $v_worktype . "'";
		$sql = $sql . ",'" . _ddmmyyyy_to_yyyymmdd($v_date)."'";
		$sql = $sql . "," . $v_result_id ;
		$sql = $sql . "," . $v_file_id;
		$sql = $sql . ",'" . $v_filename."'";
		$sql = $sql . "," . _CONST_STATUS_UPDATE_DATE ;
		$ado_conn->SetFetchMode(ADODB_FETCH_ASSOC);
		$rs = $ado_conn->GetRow($sql); 
		$v_error = _replace_bad_char(trim($rs['RET_ERROR']));
	}
	//echo $sql;exit;
}
if (!is_null($v_error) and $v_error<>""){?>
	<script>
		alert("<?php echo $v_error; ?>");
		if (_MODAL_DIALOG_MODE==1){
			window.location = "<?php echo $v_url; ?>";
		}else{
			window.history.back();
		}	
	</script><?php
	exit;
}
sleep(0);
?>
<form action="index.php" method="post" name="f_back">
	<input type="hidden" name="fuseaction" value="DISPLAY_ALL_RECORD_WORK">
	<input type="hidden" name="hdn_record_id"  value="<?php echo $v_record_id; ?>">
	<input type="hidden" name="hdn_recordtype_filter" value="<?php echo $hdn_recordtype_filter;?>">
	<input type="hidden" name="hdn_item_id" value="<?php echo $v_record_work_id;?>">
</form>
<Script language="javascript">
	document.forms(0).submit();
</Script>
